package cc.messcat.service.helpTeam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import cc.messcat.common.model.Pager;
import cc.messcat.common.service.BaseServiceImpl;
import cc.messcat.common.util.BeanUtil;
import cc.messcat.dao.helpTeam.HelpTeamDao;
import cc.messcat.entity.AreaVO;
import cc.messcat.entity.area.Area;
import cc.messcat.entity.helpUnits.HelpTeam;
import cc.messcat.entity.helpUnits.HelpTeamPerson;
import cc.messcat.entity.level.UserLevel;
import cc.messcat.entity.system.McUser;
import cc.messcat.util.LevelUtil;

@Service("helpTeamService")
public class HelpTeamServiceImpl extends BaseServiceImpl implements HelpTeamService {
    @Resource
    private HelpTeamDao helpTeamDao;


    @Override
    public void addHelpTeamPerson(HelpTeamPerson helpTeamPerson) {
        helpTeamPerson.setStatus("1");
        this.addObject(helpTeamPerson);
    }

    @Override
    public HelpTeam getTeamByCondition(HelpTeam team) {
        return helpTeamDao.getTeamByCondition(team);
    }

    @Override
    @Deprecated
    public Pager getPersonByVilliagesId(HelpTeamPerson helpTeamPerson, Integer pageNo, Integer pageSize) {
        List<HelpTeamPerson> result = helpTeamDao.getPersonByCondition(helpTeamPerson);
        return getPager(pageNo, pageSize, result, result.size());
    }

    @Override
    public Pager getPersonByVilliagesId(HelpTeamPerson helpTeamPerson, Long[] villiages, Integer pageNo, Integer pageSize) {
        List<HelpTeamPerson> result = helpTeamDao.getPersonByVillageIds(helpTeamPerson, villiages);
        return getPager(pageNo, pageSize, result, result.size());
    }

    @Override
    public void removeHelpTeamPersonById(Long id) {
        HelpTeamPerson htp = (HelpTeamPerson) this.retrieveObject(HelpTeamPerson.class, id);
        if (htp != null) {
            htp.setStatus("0");
            this.modifyObject(htp);
        }
    }

    @Override
    public Pager getPersonByCondition(HelpTeamPerson helpTeamPerson, AreaVO areaVO, Integer pageNo, Integer pageSize) {

        List<HelpTeamPerson> helpTeamPeoples = helpTeamDao.getPersonsByCondition(helpTeamPerson, areaVO);
        return getPager(pageNo, pageSize, helpTeamPeoples, helpTeamPeoples.size());
    }

	/**
	 * 重载添加权限 
	 */
	@Override
	@SuppressWarnings("all")
	public Pager getPersonByCondition(McUser user, Area area, HelpTeamPerson helpTeamPerson, AreaVO areaVO, Integer pageNo,
		Integer pageSize) {
		Area userArea = user.getArea();
		UserLevel userLevel = user.getUserLevel();
		List<HelpTeamPerson> helpTeamPeoples = new ArrayList<>();
		if(userLevel == null || userLevel.getLevel() < LevelUtil.ALL || userLevel.getLevel() > LevelUtil.VILLAGE) {
			return new Pager(pageSize,pageNo,0,new ArrayList<>(0));
		}
		if(userLevel.getLevel() < LevelUtil.TOWN) {
			String areaCode = StringUtils.isBlank(areaVO.getCry()) ? "%" : areaVO.getCry().length() > 8 ? areaVO.getCry().substring(0, 9) + "%" : "none";
			Map condition = new HashMap<>();
			if(helpTeamPerson.getTeam() != null && helpTeamPerson.getTeam().getVillage() != null && helpTeamPerson.getTeam().getVillage().getId() != null ) {
				condition.put("villageId", helpTeamPerson.getTeam().getVillage().getId().toString());
			}
			helpTeamPeoples = helpTeamDao.getPersonByCondition(pageNo, pageSize, helpTeamPerson, areaCode,condition);
			return getPager(pageNo, pageSize, helpTeamPeoples, helpTeamPeoples.size());
		}
		
		if (userLevel.getLevel() == LevelUtil.TOWN && userArea != null) {
			String areaCode = userArea.getAreaCode().length() > 8 ? userArea.getAreaCode().substring(0, 9) + "%" : "none";
			Map condition = new HashMap<>();
			if (BeanUtil.isValid(areaVO.getCry())) {
				areaCode = areaVO.getCry().contains(areaCode.substring(0,9)) ? areaVO.getCry().substring(0, 9) + "%" : "none";
			}
			if (BeanUtil.isValid(areaVO.getName())) {
				condition.put("village",areaVO.getName());
			}
			helpTeamPeoples = helpTeamDao.getPersonByCondition(pageNo, pageSize, helpTeamPerson, areaCode,condition);
			return getPager(pageNo, pageSize, helpTeamPeoples, helpTeamPeoples.size());
		}
	
		if(userLevel.getLevel() == LevelUtil.VILLAGE && userArea != null) {
			helpTeamPeoples = helpTeamDao.getPersonByCondition(pageNo,pageSize,helpTeamPerson,userArea.getAreaCode(),null);
			return getPager(pageNo, pageSize, helpTeamPeoples, helpTeamPeoples.size());
		}
		
		return new Pager(pageSize,pageNo,0,new ArrayList<>(0));
	}

	@Override
	public List<Map<String, Object>> getHelpTeamPersonByPoorVillage(Long id) {
		List<Map<String, Object>> list = helpTeamDao.getHelpTeamPersonByPoorVillage(id);
		return list;
	}

}
